/*

package com.schoolai.util.tools;

import java.io.File;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;

*/
/**
 * Copyright(C),2019-2025，XX公司
 * FileName:JWTDemo
 * Author:
 * 创建时间：2025/10/21 11:33
 * Description:
 * History:
 * <auth>        <time>       <version>       <desc>
 * 作者          修改时间       版本号         描述
 *//*



public class JWTDemo {
    public static void main(String[] args) {
        try {
            // JKS文件路径和配置
            String jksFilePath = "D:/work/school-ai/school-util/src/main/resources/oauth2.jks";
            String publicKeyFilePath = "D:/work/school-ai/school-util/src/main/resources/public.txt";
            String alias = "oauth2";
            String password = "oauth2";

            // 1. 首先生成密钥文件（如果不存在）
            File jksFile = new File(jksFilePath);
            if (!jksFile.exists()) {
                System.out.println("生成新的密钥文件...");
               // KeyGenerator.generateKeys(jksFilePath, alias, password, publicKeyFilePath);
            }

            // 2. 从JKS文件加载私钥
            System.out.println("从JKS文件加载私钥...");
            RSAPrivateKey privateKey = JWTUtil.loadPrivateKeyFromJKS(jksFilePath, alias, password);

            // 3. 从文本文件加载公钥
            System.out.println("从文本文件加载公钥...");
            RSAPublicKey publicKey = JWTUtil.loadPublicKeyFromFile(publicKeyFilePath);
            JWTUtil jwtUtil = new JWTUtil();
            // 5. 准备自定义声明
            Map<String, Object> customClaims = new HashMap<>();
            customClaims.put("username", "john_doe");
            customClaims.put("role", "admin");
            customClaims.put("email", "john@example.com");
            customClaims.put("department", "engineering");

            // 6. 生成JWT令牌（有效期1小时）
            System.out.println("生成JWT令牌...");
            String token = jwtUtil.generateToken(
                    privateKey,
                    "user123",
                    "my-auth-service",
                    3600000, // 1小时
                    customClaims
            );

            System.out.println("生成的JWT令牌: " + token);
            System.out.println();

            // 7. 验证JWT令牌
            System.out.println("验证JWT令牌...");
            JWTUtil.VerificationResult result = jwtUtil.verifyToken(publicKey,token);

            if (result.isValid()) {
                System.out.println("✓ 令牌验证成功");
                System.out.println("主题: " + result.getClaims().getSubject());
                System.out.println("签发者: " + result.getClaims().getIssuer());
                System.out.println("用户名: " + result.getClaims().getStringClaim("username"));
                System.out.println("角色: " + result.getClaims().getStringClaim("role"));
                System.out.println("邮箱: " + result.getClaims().getStringClaim("email"));
                System.out.println("部门: " + result.getClaims().getStringClaim("department"));
                System.out.println("过期时间: " + result.getClaims().getExpirationTime());
            } else {
                System.out.println("✗ 令牌验证失败: " + result.getMessage());
            }


           // 8. 演示使用JKS中的公钥进行验证
            System.out.println();
            System.out.println("使用JKS中的公钥进行验证...");
            RSAPublicKey publicKeyFromJKS = JWTUtil.loadPublicKeyFromJKS(jksFilePath, alias, password);
            JWTUtil jwtUtil2 = new JWTUtil(null, publicKeyFromJKS);
            JWTUtil.VerificationResult result2 = jwtUtil2.verifyToken(token);

            if (result2.isValid()) {
                System.out.println("✓ JKS公钥验证成功");
            } else {
                System.out.println("✗ JKS公钥验证失败: " + result2.getMessage());
            }


        } catch (Exception e) {
            System.err.println("发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
*/
